<template>
  <el-dialog :visible="showRoleDialog" append-to-body title="分配角色" @close="closeDialog">

    <el-checkbox-group v-model="roleIds">
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <!-- {{ curId }} -->
    <el-button type="primary" style="margin-top:10px" @click="hSave">确认</el-button>
  </el-dialog>
</template>

<script>
import { getRoleList, assignRoles } from '@/api/role'
import { getEmployeeDetail } from '@/api/employee'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      required: true
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    closeDialog() {
      this.$emit('update:showRoleDialog', false) // 通知父亲关闭弹窗
    },
    async loadRoles() { // 获取角色列表
      const res = await getRoleList()
      //   console.log(res)
      this.roleList = res.data
    },
    async getRoleDeta() {
      const res = await getEmployeeDetail(this.curId)
      //   console.log(res.data.roleIds)
      this.roleIds = res.data.roleIds
    },
    async hSave() {
    //   console.log('点击确认')
      await assignRoles({ id: this.curId, roleIds: this.roleIds })
      this.$message.success('给用户分配角色成功')
      this.closeDialog()
    }
  }
}
</script>
